package codingforgreat.lchot150;

public class Lc160 {
 public class ListNode {
     int val;
     ListNode next;
     ListNode(int x) {
         val = x;
         next = null;
     }
 }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode dummy = headA;
        int count = 0;
        while(dummy != null){
            dummy = dummy.next;
            ++count;
        }
        dummy = headB;
        while(dummy != null){
            dummy = dummy.next;
            --count;
        }
        ListNode l = (count >= 0) ? headA : headB ;
        ListNode s = l == headA ? headB : headA;
        for(int i = 0;i < Math.abs(count);i++){
            l = l.next;
        }
        while(l != null && l != s){
            l = l.next;
            s = s.next;
        }
        return l == s ? l : null;
    }
}
